package com.obrempon.credit.market.ui.adapter

import android.view.View
import androidx.recyclerview.widget.RecyclerView
import com.obrempon.credit.market.R
import com.obrempon.module.adapter.ObremponRecyclerViewAdapter
import com.obrempon.module.adapter.ObremponViewHolderHelper
import org.json.JSONObject

/**
 * ObremponCredit
 * @Author wwdeng
 * @CreateTime: 2024/11/19 10:43
 * @Annotation：订单列表适配器
 */
class ObremponPermissionApt(recyclerView: RecyclerView?) : ObremponRecyclerViewAdapter<JSONObject>(recyclerView,R.layout.obrempon_permission_item) {

    override fun setItemChildListener(helper: ObremponViewHolderHelper, viewType: Int) {
        helper.setItemChildClickListener(R.id.buttonGoAgree)
    }

    override fun fillData(helper: ObremponViewHolderHelper, position: Int, model: JSONObject) {
        val imgIcon = helper.getImageView(R.id.imgIcon)
        imgIcon.setImageResource(model.optInt("icon"))
        val textPermissionTitle = helper.getTextView(R.id.textPermissionTitle)
        textPermissionTitle.text = model.optString("title")

        helper.setText(R.id.textPassRate," + ${model.optString("passRate")}"+"%")
        helper.setText(R.id.textCreditLine," + ${model.optString("creditLine")}")

        val textPermissionStatus = helper.getTextView(R.id.textPermissionStatus)
        val imgPermissionStatus = helper.getImageView(R.id.imgPermissionStatus)

        helper.setVisibility(R.id.creditLineLayout,View.VISIBLE)
        helper.setVisibility(R.id.passRateLayout,View.VISIBLE)
        helper.setVisibility(R.id.buttonGoAgree,View.VISIBLE)

        //是否是系统权限
        val isSystem = model.optBoolean("isSystem")
        when(isSystem){
            true -> {
                val isAgree = model.optBoolean("isAgree")
                if (isAgree){//已授权
                    textPermissionTitle.setTextColor(getColor(R.color.black))
                    textPermissionStatus.visibility = View.VISIBLE
                    textPermissionStatus.text = getString(R.string.obrempon_permission_agreed)
                    textPermissionStatus.setTextColor(getColor(R.color.color_ff4fc351))
                    imgPermissionStatus.setImageResource(R.mipmap.obrempon_permission_agreed_icon)

                    helper.setVisibility(R.id.creditLineLayout,View.GONE)
                    helper.setVisibility(R.id.passRateLayout,View.GONE)
                    helper.setVisibility(R.id.buttonGoAgree,View.GONE)
                } else {//未授权，包含：未申请，已拒绝
                    val isDisagree = model.optBoolean("disagree")
                    if (isDisagree){//已拒绝
                        textPermissionTitle.setTextColor(getColor(R.color.color_ffff6a4c))
                        textPermissionStatus.visibility = View.VISIBLE
                        textPermissionStatus.text = getString(R.string.obrempon_permission_disagree)
                        textPermissionStatus.setTextColor(getColor(R.color.color_ffff6a4c))
                        imgPermissionStatus.setImageResource(R.mipmap.obrempon_permission_disagree_icon)
                    } else {
                        textPermissionTitle.setTextColor(getColor(R.color.black))
                        textPermissionStatus.visibility = View.GONE
                        imgPermissionStatus.setImageResource(R.mipmap.obrempon_permission_default_icon)
                    }
                }
            }
            else -> {//非系统权限，判断是否已阅读
                val isRead = model.optBoolean("isRead")
                if (isRead){
                    textPermissionTitle.setTextColor(getColor(R.color.black))
                    textPermissionStatus.visibility = View.VISIBLE
                    textPermissionStatus.text = getString(R.string.obrempon_permission_agreed)
                    textPermissionStatus.setTextColor(getColor(R.color.color_ff4fc351))
                    imgPermissionStatus.setImageResource(R.mipmap.obrempon_permission_agreed_icon)

                    helper.setVisibility(R.id.creditLineLayout,View.GONE)
                    helper.setVisibility(R.id.passRateLayout,View.GONE)
                    helper.setVisibility(R.id.buttonGoAgree,View.GONE)
                } else {
                    textPermissionTitle.setTextColor(getColor(R.color.black))
                    textPermissionStatus.visibility = View.GONE
                    imgPermissionStatus.setImageResource(R.mipmap.obrempon_permission_default_icon)
                }
            }
        }

        when(position == itemCount - 1){
            true -> {
                helper.setVisibility(R.id.viewPermissionLine, View.INVISIBLE)
            }
            else -> {
                helper.setVisibility(R.id.viewPermissionLine, View.VISIBLE)
            }
        }
    }
}